using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace whereIsTheDonutGame
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class Trigger : Microsoft.Xna.Framework.DrawableGameComponent
    {
        private float x, z;
        public donutGame game;
        private float trans;
        private ICamera camera;
        Model model;
        public BoundingBox boundingBox { get; set; }
        public Boolean triggered;
        public Trigger(donutGame game, float x, float z)
            : base(game)
        {
            triggered = false;
            this.game = game;
            trans = 0;
            this.x = x;
            this.z = z;
            boundingBox = new BoundingBox(new Vector3(x - 2, game.terrain.Height[(int)x, (int)z] - 2, -z - 2), new Vector3(x + 2, game.terrain.Height[(int)x, (int)z] + 2, -z + 2));
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            camera = (ICamera)Game.Services.GetService(typeof(ICamera));

            base.Initialize();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            Boolean trigg = false;
            foreach(MovableBox b in game.cspawn.boxes){
                if (b.boundingBox.Intersects(boundingBox))
                {
                    trigg = true;
                    break;
                }
            }
            if (game.player.bbox.Intersects(boundingBox))
                trigg = true;

            if (trigg == false)
            {
                triggered = false;
                trans = 0;
            }
            else
            {
                triggered = true;
                trans = 0.4f;
            }
            base.Update(gameTime);
        }

        public override void Draw(Microsoft.Xna.Framework.GameTime gameTime)
        {
            GraphicsDevice.BlendState = BlendState.Opaque;
            GraphicsDevice.DepthStencilState = DepthStencilState.Default;
            foreach (ModelMesh mesh in model.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.EnableDefaultLighting();
                    effect.View = camera.ViewMatrix;
                    effect.Projection = camera.ProjectionMatrix;
                    effect.World = Matrix.CreateTranslation(new Vector3(x, game.terrain.Height[(int)x,(int) z] - trans + 0.5f, -z));
                }
                mesh.Draw();
            }
            base.Draw(gameTime);
        }

        protected override void LoadContent()
        {
            model = this.Game.Content.Load<Model>("plate");
        }
    }
}
